package com.demo.web.admin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.demo.web.admin.interceptor.AuthInterceptor;

/**
 * @author Administrator
 *自动配置默认配置 WebMvcAutoConfiguration
 *无需使用 @EnableWebMvc 注解,如果使用了该注解,所有web mvc的配置将需要自己手动完成
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
	
	
//	@Bean
//	public MyFilter myFilter() {
//		return new MyFilter();
//	}
//	
//	@Bean
//    public FilterRegistrationBean indexFilterRegistration() {
//        FilterRegistrationBean registration = new FilterRegistrationBean(myFilter());
//        registration.addUrlPatterns("/*");
//        return registration;
//    }
	
//	@Bean
//    public ServletRegistrationBean servletRegistrationBean() {
//		ServletRegistrationBean registration = new ServletRegistrationBean(new MyServlet());
//		registration.addUrlMappings("/myServlet");
//        return registration;
//    }
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//添加的顺序决定了拦截的先后顺序
		registry.addInterceptor(authInterceptor()).addPathPatterns("/**");
		super.addInterceptors(registry);
	}
	
	@Bean
	public  AuthInterceptor authInterceptor() {
		return new AuthInterceptor();
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
//		registry.addResourceHandler("/myres/**").addResourceLocations("classpath:/myres/");
//		registry.addResourceHandler("/myimgs/**").addResourceLocations("file:D:/myimgs/");
		super.addResourceHandlers(registry);
	}

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		//首页设置
		registry.addViewController("/").setViewName("forward:/index");
//		registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
		super.addViewControllers(registry);
	}
	
}
